package entity;
import java.io.UnsupportedEncodingException;

import org.msgpack.annotation.Index;
import org.msgpack.annotation.Message;

import Util.Util;
 
@Message
public class ChatMessage {
    @Index(0)
    private String sendUser;
    @Index(1)
    private String receiveUser;
    @Index(2)
    private String message;
    @Index(3)
    private int messagetype;//1:��ʼ����֤��Ϣ��2��������Ϣ
 
    
    public byte[] encode() throws UnsupportedEncodingException {
        int sendUserLength = sendUser.getBytes("UTF-8").length;
        int receiveUserLength = receiveUser.getBytes("UTF-8").length;
        int messageLength = message.getBytes("UTF-8").length;
        int totalLength = 4 + sendUserLength + 4 + receiveUserLength + 4 + messageLength + 4;
        byte[] buffer = new byte[totalLength];
        int offset = 0;

        // ǰ�ĸ��ֽڴ洢sendUser�ĳ���
        System.arraycopy(Util.int2bytes(sendUserLength), 0, buffer, offset, 4);
        offset += 4;
        // copy send user to buffer
        System.arraycopy(sendUser.getBytes("UTF-8"), 0, buffer, offset, sendUserLength);
        offset += sendUserLength;

        System.arraycopy(Util.int2bytes(receiveUserLength), 0, buffer, offset, 4);
        offset += 4;

        System.arraycopy(receiveUser.getBytes("UTF-8"), 0, buffer, offset, receiveUserLength );
        offset += receiveUserLength;

        System.arraycopy(Util.int2bytes(messageLength), 0, buffer, offset, 4);
        offset += 4;

        System.arraycopy(message.getBytes("UTF-8"), 0, buffer, offset, messageLength);
        offset += messageLength;
        // copy sign to buffer

        byte[] messageTypeBuffer = Util.int2bytes(messagetype);
        System.arraycopy(messageTypeBuffer, 0, buffer, offset, 4);

        return buffer;
    }
    public void decode(byte[] buffer) throws UnsupportedEncodingException {
        int start_pos = 0;
        // decode send user

    	// ��ȡ����
    	int length = Util.bytes2int(buffer, start_pos);
    	start_pos += 4;
        this.sendUser = new String(buffer, start_pos, length, "UTF-8");
        start_pos += length;


        length = Util.bytes2int(buffer, start_pos);

    	start_pos += 4;
        this.receiveUser = new String(buffer, start_pos, length, "UTF-8");
        start_pos += length;

     
        length = Util.bytes2int(buffer, start_pos);

    	start_pos += 4;
        this.message = new String(buffer, start_pos, length, "UTF-8");
        start_pos += length;

        
        this.messagetype = Util.bytes2int(buffer, start_pos);
        System.out.println();
    }
    
    public ChatMessage() {
    }
 
    public ChatMessage(String sendUser, String receiveUser, String message, int messagetype){
        this.sendUser=sendUser;
        this.receiveUser=receiveUser;
        this.message=message;
        this.messagetype=messagetype;
    }
 
    public String getSendUser() {
        return sendUser;
    }
 
    public void setSendUser(String sendUser) {
        this.sendUser = sendUser;
    }
 
    public String getReceiveUser() {
        return receiveUser;
    }
 
    public void setReceiveUser(String receiveUser) {
        this.receiveUser = receiveUser;
    }
 
    public String getMessage() {
        return message;
    }
 
    public void setMessage(String message) {
        this.message = message;
    }
 
    public int getMessagetype() {
        return messagetype;
    }
 
    public void setMessagetype(int messagetype) {
        this.messagetype = messagetype;
    }
 
    @Override
    public String toString() {
        return "ChatMessage{" +
                "sendUser='" + sendUser + '\'' +
                ", receiveUser='" + receiveUser + '\'' +
                ", message='" + message + '\'' +
                ", messageType='" + messagetype + '\'' +
                '}';
    }
}